<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">设备所属类型</label>
                        <div class="layui-input-block">
                            <div name="device_type_id" id="device_type_id" required value="1" ></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">所属型号</label>
                        <div class="layui-input-block">
                            <div name="model" id="model_id" required value="0" ></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">设备名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item" style="display: none" id="lecp_server">
                        <label class="layui-form-label required">是否启用LECPServer</label>
                        <div class="layui-input-block">
                            <div name="is_lecp_server" id="is_lecp_server" value="1" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item" style="display: none" id="lecp_server_name">
                        <label class="layui-form-label required">LECPServer服务名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="lecp_server_name" value=""  class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="device_sn" style="display: none">
                        <label class="layui-form-label required">设备编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="device_sn" value=""  class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="device_ip">
                        <label class="layui-form-label">设备ip</label>
                        <div class="layui-input-block">
                            <input type="text" name="ip" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="device_port">
                        <label class="layui-form-label">设备端口</label>
                        <div class="layui-input-block">
                            <input type="number" name="port" value="" class="layui-input">
                        </div>
                    </div>



                    <div class="layui-form-item" id="device_serial" style="display: none">
                        <label class="layui-form-label required">串口</label>
                        <div class="layui-input-block">
                            <input type="text" name="device_sn" id="device_serial_sn" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">经度</label>
                        <div class="layui-input-block">
                            <input type="text" name="lon" id="lon" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">纬度</label>
                        <div class="layui-input-block">
                            <input type="text" name="lat" id="lat" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">添加时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="created_time" id="created_time" required lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>

        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>

        <script>
            function generateRandomLatLng() {
                const randomLat = () => (Math.random() * 180) - 90; // 生成-90到90的随机数
                const randomLng = () => (Math.random() * 360) - 180; // 生成-180到180的随机数
                return {
                    latitude: randomLat(),
                    longitude: randomLng()
                };
            }
            // 使用函数生成一个经纬度对象
            const latLng = generateRandomLatLng();
            document.getElementById("lat").value=latLng.latitude;
            document.getElementById("lon").value=latLng.longitude;
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    const latitude = position.coords.latitude;
                    const longitude = position.coords.longitude;
                    document.getElementById("lat").value=latitude;
                    document.getElementById("lon").value=longitude;
                });
            }

            function get_uuid() {
                const s = [];
                const hexDigits = "0123456789abcdef";
                for (let i = 0; i < 36; i++) {
                    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
                }
                s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
                s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
                s[8] = s[13] = s[18] = s[23] = "-";
                return s.join("");
            }
            // 相关接口
            const INSERT_API = "/app/admin/iot-device/insert";

            // 字段 created_time created_time
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_time",
                    type: "datetime",
                });
            })
            //串口列表
            layui.use(["jquery", "xmSelect"], function () {
                let value = layui.$("#device_serial_sn").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#device_serial_sn",
                    name: "device_sn",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value": "1", "name": "点位"}, {"value": "2", "name": "自定义"}],
                    value: "1",
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                })
            });

            layui.use(["jquery", "xmSelect"], function () {
                let value = layui.$("#is_lecp_server").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#is_lecp_server",
                    name: "is_lecp_server",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value": "0", "name": "是"}, {"value": "1", "name": "否"}],
                    value: "1",
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    on:function (data) {
                        //获取类型的id
                        const is_lecp_server = data.change[0].value;
                        if (is_lecp_server==0){
                            document.getElementById("lecp_server_name").style.display = "block";
                        }else{
                            document.getElementById("lecp_server_name").style.display = "none";
                        }
                    }
                })
            });

            // 字段 所属型号 model_id
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.xmSelect.render({
                    el: "#model_id",
                    name: "model",
                    initValue: 0,
                    filterable: true,
                    data: [],
                    value: "0",
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "required",
                });
            });

            // 字段 设备所属类型 device_type_id
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/iot-device-type/select?format=tree",
                    dataType: "json",
                    success: function (res) {
                        let value = layui.$("#device_type_id").attr("value");
                        let initValue = value ? value.split(",") : [];
                        layui.xmSelect.render({
                            el: "#device_type_id",
                            name: "device_type_id",
                            initValue: initValue,
                            filterable: true,
                            data: res.data,
                            value: "0",
                            model: {"icon": "hidden", "label": {"type": "text"}},
                            clickClose: true,
                            radio: true,
                            linkage: true,
                            tree: {
                                show: true,
                                strict: false,
                                clickCheck: true,
                                clickExpand: false,
                                expandedKeys: initValue
                            },
                            layVerify: "required",
                            //联动所属型号
                            on:function (data) {
                                //获取类型的id
                                const type_name = data.change[0].name;
                                let preoccupation = ["PLC","西门子","欧姆龙"];
                                if (preoccupation.includes(type_name)){
                                    document.getElementById("lecp_server").style.display = "block";
                                }else{
                                    document.getElementById("lecp_server").style.display = "none";
                                }

                                if (type_name.includes('Rtu') || type_name.includes('rtu')|| type_name.includes('RTU')){
                                    document.getElementById("device_sn").style.display = "none";
                                    document.getElementById("device_port").style.display = "none";
                                    document.getElementById("device_ip").style.display = "none";
                                    document.getElementById("device_serial").style.display = "block";
                                }else{
                                    document.getElementById("device_sn").style.display = "block";
                                    document.getElementById("device_port").style.display = "block";
                                    document.getElementById("device_ip").style.display = "block";
                                    document.getElementById("device_serial").style.display = "none";
                                }

                                //获取类型的id
                                const type_id = data.change[0].value;
                                layui.$.ajax({
                                    url: "/app/admin/iot-device-model/select?format=tree&type_id="+type_id,
                                    dataType: "json",
                                    success: function (res) {
                                        let value = layui.$("#model_id").attr("value");
                                        let initValue = value ? value.split(",") : [];
                                        layui.xmSelect.render({
                                            el: "#model_id",
                                            name: "model",
                                            initValue: initValue,
                                            filterable: true,
                                            data: res.data,
                                            value: "2",
                                            model: {"icon":"hidden","label":{"type":"text"}},
                                            clickClose: true,
                                            radio: true,
                                            layVerify: "required",
                                        });
                                        if (res.code) {
                                            layui.popup.failure(res.msg);
                                        }
                                    }
                                });
                            }
                        });
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field.uid = get_uuid();
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>
</html>
